package com.starsoft.smdc.web;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.smdc.bean.SmdcMarketDaily;
import com.starsoft.smdc.bean.SmdcMarketDailyId;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.crawler.SmdcUpdateService;
import com.starsoft.smdc.service.MarketDailyService;
import com.starsoft.smdc.service.SecurityService;

@RestController
@RequestMapping(value = "/smdc/market")
public class MarketController {

	@Autowired
	MarketDailyService marketService;
	
	@Autowired
	SecurityService securityService;
	
	@Autowired
	SmdcUpdateService updateService;
	
	@RequestMapping(value="/page")
	public Map<String, Object> pageQuery(HttpServletRequest request){
		PageObject po = WebUtil.getPageObject(request);
		String secId = request.getParameter("secId");
		Date beginDate = WebUtil.getDate(request, "beginDate", "yyyy-MM-dd");
		Date endDate = WebUtil.getDate(request, "endDate", "yyyy-MM-dd");
		return marketService.pageQuery(po, secId, beginDate, endDate);
	}
	
	@RequestMapping(value="/update")
	public Map<String, Object> update(HttpServletRequest request){
		int num=0;
		String secId = request.getParameter("secId");
		Date beginDate = WebUtil.getDate(request, "beginDate", "yyyy-MM-dd");
		if(StringUtil.isEmpty(secId)){
			num = updateService.updateMarketAll(beginDate).size();
		}else{
			SmdcSecurity security = securityService.get(secId);
			num = updateService.updateMarket(security, beginDate).size();
		}
		return WebUtil.sucesseModel("更新成功:"+num);
	}

	@RequestMapping(value = "/delete")
	public Map<String, Object> delete(HttpServletRequest request, SmdcMarketDaily market) {
		Date tradeDate = WebUtil.getDate(request, "tradeDate", "yyyy-MM-dd");
		String secId = request.getParameter("secId");
		SmdcMarketDailyId id = new SmdcMarketDailyId(secId, tradeDate);
		market.setId(id);
		marketService.delete(market);
		return WebUtil.sucesseModel(null);
	}

	@RequestMapping(value = "/save")
	public Map<String, Object> save(HttpServletRequest request, SmdcMarketDaily market) {
		Date tradeDate = WebUtil.getDate(request, "tradeDate", "yyyy-MM-dd");
		String secId = request.getParameter("secId");
		SmdcMarketDailyId id = new SmdcMarketDailyId(secId, tradeDate);
		market.setId(id);
		if (market.getClosePrice() != null && market.getPreClosePrice() != null) {
			market.setPriceChange(market.getClosePrice() - market.getPreClosePrice());
			market.setPercent(market.getPriceChange() / market.getPreClosePrice());
		} else {
			return WebUtil.errorModel("收盘价不正确");
		}
		if (market.getPeWeighted() != null && market.getPeWeighted() != null) {
			market.setRoeWeighted(market.getPbWeighted() / market.getPeWeighted());
		}
		marketService.saveNotNullFeild(market);
		return WebUtil.sucesseModel("保存成功");
	}
}
